package Top200;

import common.ListNode;

/**
 * @author zhangmin
 * @create 2022-02-18 19:45
 * 快慢指针
 */
public class removeNthFromEnd19 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if (head==null) return null;
        ListNode slow=head,fast=head;
        while (fast!=null&&n>0){
            fast=fast.next;
            n--;
        }
        if (fast==null){
            return head.next;
        }
        while (fast.next!=null){
            fast=fast.next;
            slow=slow.next;
        }
        slow.next=slow.next.next;
        return head;
    }

}
